<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/5/13
 * Time: 13:13
 */
namespace Admin\Controller;
use Admin\Controller\AdminController;

class FinanceController extends AdminController
{
    protected $excelTitle = array();

    public function orderList(){
//        M('FruitOrder','tp_','DB_WX');
        $type = I('type');
//        myDump($type);
        $data = D('Finance')->getList($type);
//        $data = I('get.');
//        echo '<pre>';
//        var_dump($data);die;
        $payWay = D('Admin/Dict')->getDict('payWay');
        $assign = array(
            'data'=>$data['data'],
            'title'=>$data['title'],
            'th'=>$data['th'],
            'page'=>$data['page'],
            'payWay'=>$payWay,
            'start_time'=>I('start_time'),
            'end_time'=>I('end_time'),
        );
        $this->assign($assign);
        $this->display();
    }

    public function detail(){
        $type = I('type');
        $id = I('id');
        $data = D('Finance')->detail($id,$type);
//        var_dump($data);die;
        $this->assign('data',$data);
        $this->display();
    }

    public function dumpExcel(){
        $type = I('type');
//        myDump($type);die;
        $model = D('Finance');
        $all = $model->getList($type, false, 99999);
        $_data = $all['data'];
        if ($all['type']==1) {
            foreach ($_data as $k => $v) {
                $middle = $model->detail($v['id'], $v['order_type']);
                $data[$k]['orderid'] = $middle['orderid'];
                $data[$k]['time'] = date('Y-m-d', $middle['time']);
                $data[$k]['name'] = $middle['name'];
                $data[$k]['tel'] = $middle['tel'];
                $data[$k]['order_type'] = $middle['order_type'];
                $data[$k]['price'] = $middle['price'];
                $data[$k]['info'] = $middle['info'];
                $data[$k]['address'] = $middle['address'];
                $data[$k]['isuse'] = $middle['isuse'];
                $data[$k]['paytype'] = $middle['paytype'];
                $data[$k]['stype'] = $middle['stype'];
                $data[$k]['isrefund'] = $middle['isrefund'];
            }
            $title = array('账单号','创建时间','客户姓名','联系方式','账单类型','金额','商品类型','地址','发货状态','付款方式','外送方式','是否退货');
        }elseif ($all['type']==2) {

            foreach ($_data as $k => $v) {
                $data[$k]['no'] = '编号'.$v['no'];
                $data[$k]['pay_time'] = date('Y-m-d', $v['pay_time']);
                $data[$k]['payer_name'] = $v['payer_name'];
                $data[$k]['householder_name'] = $v['householder_name'];
                $data[$k]['tel'] = $v['tel'];
                $data[$k]['pay_way'] = $v['pay_way'];
                $data[$k]['pay_money'] = $v['pay_money'];
                $data[$k]['room_name'] = $v['room_name'];
//                    myDump($v['accountDetail']);die;
                foreach ($v['accountDetail'] as $kk =>&$vv) {
                    if (!$this->excelTitle['th'][$vv['type']] && $vv['type']) {
                        $vv['sort'] = $this->excelTitle['th'][$vv['type']] = $this->excelTitle['num']?$this->excelTitle['num']:1;
                        $this->excelTitle['num'] || $this->excelTitle['num']=1;
                        $this->excelTitle['num']++;
                    } else {
                        $vv['sort'] == $this->excelTitle['th'][$vv['type']];
                    }
                    if($data[$k]['accountDetail'][$vv['type']])
                        $data[$k]['accountDetail'][$vv['type']] .= ','.$vv['money'];
                    else
                        $data[$k]['accountDetail'][$vv['type']] = $vv['money'];
                }
            }
//            myDump($data);
            $other = array();
            foreach ($data as $k => $v) {
                foreach ($this->excelTitle['th'] as $kk=>$vv){
                    $has = false;
                    foreach ($v['accountDetail'] as $kkk=>$vvv) {
                        if($kkk==$kk) {
                            if($other[$k][$kk]){
                                $other[$k][$kk] .= ','.$vvv;
                            }else {
                                $other[$k][$kk] = $vvv;
                            }
                            $has = 1;
                        }
                    }
                    if(!$has)
                        $other[$k][] = '';
                }
                unset($data[$k]['accountDetail']);
            }
            $title = array('账单号','支付时间','缴费人姓名','户主姓名','户主联系方式','支付方式','金额','房间地址');
            foreach ($this->excelTitle['th'] as $k=>$v) {
                $title[] = $k;
            }
        }
        foreach ($data as $k=>&$v) {
            $v = array_merge($v,$other[$k]);
        }

//        myDump($this->excelTitle);
//        myDump($other);
//        myDump($data);die;
        dumpExcel($title, $data,'费用账单');
    }

}